package Z_TopicPattern;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import utils.RabbitMQUtils;
import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author： pang-yun
 * @date： 2021-12-20 14:05
 * 功能目的 ：用于
 */

public class TopicConsumerA {
    public static void main(String[] args) throws IOException, TimeoutException {
        //获取信道
        Channel channel = RabbitMQUtils.getChannel();
        //声明交换机
        channel.exchangeDeclare(TopicConst.EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        //声明队列
        channel.queueDeclare(TopicConst.QUEUE_NAME_A, false, false, false, null);
        //绑定 队列 交换机
        channel.queueBind(TopicConst.QUEUE_NAME_A, TopicConst.EXCHANGE_NAME, TopicConst.ROUTE_KEY_A);

        //-------------回调函数---------------------------------
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("message ： " + new String(message.getBody()) + '\n' +
                    "routeKey :" + message.getEnvelope().getRoutingKey());
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("message Failed");
        };

        // 消费消息
        channel.basicConsume(TopicConst.QUEUE_NAME_A, true, deliverCallback, cancelCallback);

    }
}
